public class OTResourceAllcation {
    public Activity_Information__c ActivityInit{get;set;}     
    public CommonMethod getCommonMethod;
    public List<ActivityCompetitionBrandEntry> CompetitionBrandList{get;set;}//竞品信息
    public List<Activity_Competition_Brand_Detail__c> CompetitionBrandDetailList{get;set;}
   public List<String> ETColumnList{get;set;}
   public List<String> FGColumnList{get;set;}
   public Set<String> OutletColumnList{get;set;}
   public  List<DynamicColumnEntry> DynamicColumnEntryList{get;set;}
   public  List<DynamicColumnEntry> FGDynamicColumnEntryList{get;set;}
   public Boolean IsEnableForActionButton{get;set;}//按钮是否可用
   //public List<Resource_Allocation_Detail__c> ETresourceDetailList;//ET资源明细
   public List<CityResourceAllcation__c> FGresourceDetailList;//FG资源明细
   public List<CityResourceAllcation__c> ETResourceDetailList;//存放OT每个城市的资源信息
   public Boolean IsDisplayZhanbi{get;set;}//占比计算是否显示字段
   string UserRegion;//当前用户所在的区域
   public List<DynamicColumnEntry> KAResourceDynamicEntryList{get;set;}//KA分配信息
   public List<String> KAColumnList{get;set;}//KA资源动态列
  public List<DynamicColumnEntry> TotalTargetList{get;set;}
   
   //是否是SSY 或者 Region活动
    public Boolean IsSSYorRegion{
      get{
         Boolean flag=false;
          if(ActivityInit.Activity_Type__c=='Region'){
             flag=true;   //暂时
          }else if(ActivityInit.Activity_Type__c=='SSY'){
            flag=false;  
          }
      return flag;
      }
      set;
      }
    public OTResourceAllcation(ApexPages.StandardController controller) 
    {
         getCommonMethod=new CommonMethod ();
         UserRegion=getCommonMethod.getUserRegion();
         IsDisplayZhanbi = false;
         IsEnableForActionButton=false;
        
        KAColumnList=new List<string>();
        KAResourceDynamicEntryList=new List<DynamicColumnEntry>();
        ETresourceDetailList=new List<CityResourceAllcation__c>();
        FGresourceDetailList=new List<CityResourceAllcation__c>();
        ActivityInit=[Select Id,Name,  Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, 
                             Activity_Type_State__c, Activity_Type__c, DM_End_Date__c, DM_Start_Date__c, Display_End_Date__c, 
                             Display_Start_Date__c,Serial_Number__c FROM Activity_Information__c where id=: RecordId];
                             
         String region=getCommonMethod.getUserRegion();
         List<ParticipatorArea__c> parAreaList=new List<ParticipatorArea__c>([select OT_Status__c from ParticipatorArea__c where Activity_Name__c=:RecordId 
                                                                                                                       and Region__c=:region and OT_Status__c!='SRTM已提交']);
         //System.assert(false,parAreaList);  
       if(ActivityInit.Activity_Stage__c=='活动已取消' || ActivityInit.Activity_Stage__c=='SRTM已完成区域调整')
      {
            IsEnableForActionButton=false;
       }  
       else
       {
            Date StartDT=ActivityInit.Activity_Start_Date__c;                   
            if(Date.today()>= StartDT.addDays(-21))//开始活动前21天不可以调时间
            {   
                  IsEnableForActionButton=false;
            }      
            else
            {
             if(IsSSYorRegion){
               if(ActivityInit.Activity_Stage__c=='SRTM已发起活动')
                    IsEnableForActionButton=true;
             }else{  
                 if( parAreaList.size()>0)      
                    IsEnableForActionButton=true;      
           }
         }
      }
           TotalTargetList = new List<DynamicColumnEntry>();
            if(isSSYorRegion)
              //TotalTargetList =getCommonMethod.getPercentSaleOrRecourceForCity('RegionArea',RecordId); 
              TotalTargetList =getCommonMethod.getPercentSaleOrRecourceForCity(ActivityInit.Activity_Type__c,UserRegion,RecordId); 
           else
              TotalTargetList =getCommonMethod.getPercentSaleOrRecourceForCity(ActivityInit.Activity_Type__c,UserRegion,RecordId);    
              
         //加载KA信息
       LoadKaData();
           
         LoadOutletList();  
    }
    
 
    //ET or OT资源
     public List<AllocationResourceEntry> ResourceEntryList
     {
           get
         {   //属于Region活动的ET资源
             String filter='';
             if(IsSSYorRegion){    
                filter=' and Type__c=\'ET\'  and Region__c=\''+UserRegion+'\'';               
             }   //属于SSY活动的OT资源
             else{
                 filter=' and Type__c=\'OTCity\' and Region__c=\''+UserRegion+'\'';  
             //   filter=' and Resource_Allocation__r.OT_Allocation_Number__c!=NULL and Participator_Detail_Outlet__r.OutletResourceType__c=\'OT\'  and Participator_Detail_Outlet__r.Outlet_Name__r.OutletRegion__c=\''+getCommonMethod.getUserRegion()+'\'';               
              }
          return getCommonMethod.LoadResourceAllcationListByCity(filter,RecordId);
     }
    }
   //FG资源
     public List<AllocationResourceEntry> FGResourceEntryList
     {
           get
         {
             if(IsSSYorRegion){
               String filter=' and  Type__c=\'FG\'  and Region__c=\''+UserRegion+'\'';               
           //    String filter=' and Participator_Detail_Outlet__r.OutletResourceType__c=\'FG\'  and Participator_Detail_Outlet__r.Outlet_Name__r.OutletRegion__c=\''+getCommonMethod.getUserRegion()+'\'';               
             //  return getCommonMethod.LoadResourceAllcationList(filter,RecordId,'1');*/
              return getCommonMethod.LoadResourceAllcationListByCity(filter,RecordId);
             }else{
               return null;
            }
     }
   }
//RecordId
 public String RecordId
   {
       get
       {
           return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
       }
       set;
   }
     public void LoadOutletList()
     {
          // UserRegion
          //加载OT资源列表，by city
          String FGfilter='';
          String filter='';
           if(IsSSYorRegion){
             FGfilter+=' and Type__c=\'FG\' and Region__c=\''+UserRegion+'\'';
             filter+=' and Type__c=\'ET\' and Region__c=\''+UserRegion+'\'';
            FGResourceDetailList=getCommonMethod.LoadCityList(FGfilter,RecordId);//加载门店列表
            FGColumnList=getCommonMethod.GetDynamicETColumnList(FGResourceDetailList);//获取列名
            FGDynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(FGResourceDetailList);//获取门店列表
           }else{
            filter+=' and Type__c=\'OTCity\' and Region__c=\''+UserRegion+'\'';
           }
          
            ETResourceDetailList=getCommonMethod.LoadCityList(filter,RecordId);//加载门店列表
            ETColumnList=getCommonMethod.GetDynamicETColumnList(ETResourceDetailList);//获取列名
            DynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(ETResourceDetailList);//获取门店列表
          
          /*  List<AggregateResult> rsList=getResult();
            List<AggregateResult> OTrsList=new List<AggregateResult>();
            List<AggregateResult> ETrsList=new List<AggregateResult>();
            List<AggregateResult> FGrsList=new List<AggregateResult>();
            
            for(AggregateResult rs:rsList){
               if(rs.get('type')=='OT')
                 OTrsList.add(rs);
               else if(rs.get('type')=='ET')
                 ETrsList.add(rs);
               else if(rs.get('type')=='FG')
                 FGrsList.add(rs);
            }
              ETColumnList=new List<String>();//列
             if(IsSSYorRegion){
               ETColumnList=getCommonMethod.GetDynamicETColumnList(ETrsList);
               DynamicColumnEntryList=getCommonMethod.getDynamicColumnEntryList(ETrsList);
               FGColumnList=new List<String>();//列
               FGColumnList=getCommonMethod.GetDynamicETColumnList(FGrsList);
               FGDynamicColumnEntryList=getCommonMethod.getDynamicColumnEntryList(FGrsList);
            }else{
              ETColumnList=getCommonMethod.GetDynamicETColumnList(OTrsList);
              DynamicColumnEntryList=getCommonMethod.getDynamicColumnEntryList(OTrsList);
            }     
            */
                
   }
 
    public PageReference  Save()
    {
        SaveData();
        pageReference pr = new pageReference('/'+RecordId);     
         pr.setredirect(true);   
         return pr;
    }
    public void SaveData()
    {
          if(ActivityInit.Activity_Type__c=='SSY')
         {
          //先删除
             delete [select id from CityResourceAllcation__c where Type__c='OTCity' and Activity_Information__c=:RecordId and Region__c=:UserRegion];
             //重新保存城市分配量
             getCommonMethod.SaveCityResourceAllocation('OT',RecordId,NULL,UserRegion,ActivityInit.Activity_Type__c,DynamicColumnEntryList);
             //分配给相应的门店
              }else{
                delete [select id from CityResourceAllcation__c where Activity_Information__c=:RecordId and Region__c=:UserRegion];
                delete [select id from Resource_Allocation_Detail__c where ActivityId__c=:RecordId and Participator_Detail_Outlet__r.Region__c=:UserRegion];
                getCommonMethod.SaveCityResourceAllocation('ET',RecordId,NULL,UserRegion,ActivityInit.Activity_Type__c,DynamicColumnEntryList);
                getCommonMethod.SaveCityResourceAllocation('FG',RecordId ,NULL,UserRegion,ActivityInit.Activity_Type__c,FGDynamicColumnEntryList);
              }

   }
  public PageReference  showDisplayZhanbiAction()
  {
      TotalTargetList.clear();
      TotalTargetList =getCommonMethod.getPercentSaleOrRecourceForCity(ActivityInit.Activity_Type__c,UserRegion,RecordId);  
      return null;
  }
     //提交数据
     public PageReference  SubmitData()
     {
         SaveData();
        
         if(ActivityInit.Activity_Type__c=='SSY')
         {
             List<ParticipatorArea__c> parAreaList=[select OT_Status__c from ParticipatorArea__c where Activity_Name__c=:RecordId and Region__c=:UserRegion];
             for(ParticipatorArea__c rad:parAreaList){
               rad.OT_Status__c='SRTM已提交';//修改SRTM的提交状态
             }
            update parAreaList;
          }
          else if(ActivityInit.Activity_Type__c=='Region')
          {
              ActivityInit.Activity_Stage__c='SRTM已完成区域调整';
              update ActivityInit;
         }
       
         IsEnableForActionButton=false;
         
         //发邮件给ASM，只发给直接汇报给RSM的ASM
         String BodyString='请登陆凤凰系统，调整并且确认【'+ActivityInit.Name+'】活动的资源';
         List<String> ToAddress=getCommonMethod.getToAddress(RecordId);
          String subject='活动调整，活动名称:'+ActivityInit.Name;
          EmailHander.SendMail(BodyString,ToAddress,subject,'',null);
          pageReference pr = new pageReference('/'+RecordId);         
         pr.setredirect(true);   
         return pr;
     }
     
     public void LoadKaData()
     {
          List<AggregateResult> resultList=[select Resource_Allocation__r.Product_Name__r.Name ResourceName,KAName__c,sum(ResourceNumber__c) Total from CityResourceAllcation__c 
                                                   where Activity_Information__c=:RecordId and type__c='KACity' and Region__c=:UserRegion
                                                   group by Resource_Allocation__r.Product_Name__r.Name,KAName__c];
                                                   
         // List<CityResourceAllcation__c> kaCityResourceDetailList=getCommonMethod.LoadCityList(filter,RecordId);//加载门店列表
          KAColumnList=getCommonMethod.GetDynamicETColumnList(resultList);//获取列名
          KAResourceDynamicEntryList=getCommonMethod.GetDynamicEntryList(resultList);//获取门店列表
     }
 
}